package com.cn.kabalifeuser.ui.my.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.ui.order.fragment.MallOrderFragment;
import com.cn.kabalifeuser.ui.order.fragment.ServiceOrderFragment;
import com.flyco.tablayout.listener.OnTabSelectListener;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/18.
 * function: 服务订单和积分订单
 */
public class OrderListAcivity extends IBaseActivity implements OnTabSelectListener {

    @BindView(R.id.tv_btn_1)
    TextView tvBtn1;
    @BindView(R.id.tv_btn_2)
    TextView tvBtn2;
    @BindView(R.id.fragmentContent)
    FrameLayout fragmentContent;


    private ServiceOrderFragment serviceOrderFragment;
    private MallOrderFragment mallOrderFragment;

    private final String TAG_FRAGMENT_PSERVICE = "service";
    private final String TAG_FRAGMENT_MALL = "mall";
    /**
     * 用户选中的fragment的tag
     */
    private String selectedFragmentTag;
    private FragmentManager fragmentManager;

    @BindColor(R.color.text_909090)
    int normalTvColor;
    @BindColor(R.color.main_white)
    int selectedtVColor;
    @BindColor(R.color.text_f8f8f8)
    int normalColor;
    @BindColor(R.color.main_color)
    int selectedColor;


    Bundle bundle;
    int sign;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_order;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("订单列表");
        bundle = getIntent().getExtras();
        sign = bundle.getInt("sign");

        fragmentManager = getSupportFragmentManager();
        if (savedInstanceState != null) {
            serviceOrderFragment = (ServiceOrderFragment) fragmentManager
                    .findFragmentByTag(TAG_FRAGMENT_PSERVICE);
            mallOrderFragment = (MallOrderFragment) fragmentManager
                    .findFragmentByTag(TAG_FRAGMENT_MALL);

            selectedFragmentTag = savedInstanceState.getString("tag");
        }
        //默认选中主页
        if (sign == 1)
            selectedFragmentTag = TAG_FRAGMENT_PSERVICE;
        else selectedFragmentTag = TAG_FRAGMENT_MALL;
        setSelectedFragment(selectedFragmentTag);
        setSelectedIcon(selectedFragmentTag);
    }


    /**
     * add添加
     *
     * @param fragment
     * @param tag
     */
    private void addFragment(Fragment fragment, String tag) {
        fragmentManager.beginTransaction().add(R.id.fragmentContent, fragment, tag).commit();
    }

    /**
     * 清除导航栏所有的选中状态。
     */
    private void clearSelection() {
        tvBtn1.setTextColor(normalTvColor);
        tvBtn2.setTextColor(normalTvColor);
        tvBtn1.setBackgroundColor(normalColor);
        tvBtn2.setBackgroundColor(normalColor);

    }

    public void setSelectedIcon(String selectedIcon) {
        clearSelection();  //TODO 先清除所有状态
        switch (selectedIcon) {
            case TAG_FRAGMENT_PSERVICE:
                tvBtn1.setTextColor(selectedtVColor);
                tvBtn1.setBackgroundColor(selectedColor);
                break;
            case TAG_FRAGMENT_MALL:
                tvBtn2.setTextColor(selectedtVColor);
                tvBtn2.setBackgroundColor(selectedColor);
            default:
                break;
        }
    }

    /**
     * 显示与隐藏
     *
     * @param fragment
     */
    private void showFragment(Fragment fragment) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //先隐藏所有的fragment
        if (serviceOrderFragment != null) {
            transaction.hide(serviceOrderFragment);
        }
        if (mallOrderFragment != null) {
            transaction.hide(mallOrderFragment);
        }
        transaction.show(fragment).commit();
        selectedFragmentTag = fragment.getTag();
    }

    private void setSelectedFragment(String selectedFragmentTag) {
        switch (selectedFragmentTag) {
            case TAG_FRAGMENT_PSERVICE:
                if (serviceOrderFragment == null) {
                    serviceOrderFragment = new ServiceOrderFragment();
                    addFragment(serviceOrderFragment, TAG_FRAGMENT_PSERVICE);
                }
                showFragment(serviceOrderFragment);
                break;
            case TAG_FRAGMENT_MALL:
                if (mallOrderFragment == null) {
                    mallOrderFragment = new MallOrderFragment();
                    addFragment(mallOrderFragment, TAG_FRAGMENT_MALL);
                }
                showFragment(mallOrderFragment);
                break;

            default:
                break;
        }
    }

    private void initType(int index) {
        switch (index) {
            case 1:
                if (!TAG_FRAGMENT_PSERVICE.equals(selectedFragmentTag)) {
                    setSelectedFragment(TAG_FRAGMENT_PSERVICE);
                    setSelectedIcon(TAG_FRAGMENT_PSERVICE);
                }

                break;
            case 2:
                if (!TAG_FRAGMENT_MALL.equals(selectedFragmentTag)) {
                    setSelectedFragment(TAG_FRAGMENT_MALL);
                    setSelectedIcon(TAG_FRAGMENT_MALL);
                }
                break;
        }
    }

    @OnClick({R.id.tv_btn_1, R.id.tv_btn_2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_btn_1:
                initType(1);
                break;
            case R.id.tv_btn_2:
                initType(2);
                break;
        }
    }

    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }
}
